<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 18-5-16
 * Time: 上午10:50
 */

namespace common\components\budget;


use common\components\tools\FileHelper;
use Yurun\Util\Chinese;
use Yurun\Util\Chinese\Pinyin;

class Nos extends Bed
{
    const NAME = 'nos';
    const TITLE = '网易云对象存储';
    public $saveDir = 'mycmf/';

    public function uploadLocalFile($file)
    {
        $ext = ".".FileHelper::getExtensionName1(basename($file));
        $filename = str_replace($ext, '', basename($file));
        $filename_arr = Chinese::toPinyin($filename, Pinyin::CONVERT_MODE_PINYIN, ' ');
        $filename = $filename_arr['pinyin'][0];
        $savePath = $this->saveDir.$filename.$ext;
        $r = \Yii::$app->nos->putObjectByFilePath($savePath, $file);
        $data = $r['@metadata'];
        $api_resp = new ApiResp();
        $api_resp->filename = $filename.$ext;
        $api_resp->storename = $filename.$ext;
        $api_resp->path = $savePath;
        $api_resp->uploaded_at = strtotime($data['headers']['date']);
        $api_resp->url = $data['effectiveUri'];
        $api_resp->delete_url = $savePath;
        $api_resp->content_type = $data['headers']['content-type'];
        return $api_resp;
    }

    public function deleteByUrl($delete_url)
    {
        \Yii::$app->nos->deleteObject($delete_url);
    }

    public function uploadFormUrl($url)
    {

    }

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub
        if (YII_ENV_DEV){
            $this->saveDir = 'test/'.$this->saveDir;
        }
    }
}